package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecLessonLessonUpdController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        String name = StringTrimUtil.trim(reqModel.name);
        String summary = StringTrimUtil.trim(reqModel.summary);
        String cover = reqModel.cover;
        String catalog1 = reqModel.catalog1;
        String catalog2 = reqModel.catalog2;
        String catalog3 = reqModel.catalog3;
        String catalog4 = reqModel.catalog4;
        String catalog5 = reqModel.catalog5;

        // 没有上传封面图片
        if (StringUtils.isBlank(cover)) {
            // 0-2的随机数
            java.util.Random random = new java.util.Random();
            int i = random.nextInt(3);
            if (i == 1) {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/11/16d9e830201cfd2658b8128e7c41fa49.jpg";
            } else if (i == 2) {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/11/e5f6509d3a9ee3193d259dd80dbc4adc.jpg";
            } else {
                cover = "https://yxyosc.91yunxiao.com/yxyosc/2018/01/12/4c3e5c520a019c3701287e96c9cfd682.jpg";
            }
        }

        YxyLessonsCloudLesson lesson = lessonRepository.findById(id);
        if (null != lesson) {
            lesson.setName(name);
            lesson.setSummary(summary);
            lesson.setCover(cover);
            lesson.setCatalogId1(catalog1);
            lesson.setCatalogId2(catalog2);
            lesson.setCatalogId3(catalog3);
            lesson.setCatalogId4(catalog4);
            lesson.setCatalogId5(catalog5);
            lesson.setUpdateDate(new Date());
            lessonRepository.saveAndFlush(lesson);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String id;
        public String name;
        public String summary;
        public String cover;
        public String catalog1;
        public String catalog2;
        public String catalog3;
        public String catalog4;
        public String catalog5;

        @Override
        public void autoCheck() {

        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String lessonId;
    }
}
